package com.ruoyi.stock2.utils;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;

public class EmailSend implements Runnable {
    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see Thread#run()
     */

    private Address[] address;
    public static Address[] addressbbc;
    private String title;
    private String content1;
    private String content2;



    public EmailSend(Address[] address ,Address[] addressbbc, String title, String content1,  String content2) {
        this.address = address;
        this.addressbbc = addressbbc;
        this.title = title;
        this.content1 = content1;
        this.content2 = content2;

    }
    public EmailSend() {

    }
    @Override
    public void run() {

        publicSendMail(address,title,content1);
        //publicSendMail(address,"旧版" + title ,content2);
    }

    public void publicSendMail(Address[] address, String subject, String content){
        System.out.println(content);

        String from = "359642959@qq.com";// 发件人电子邮箱
        String host = "smtp.qq.com"; // 指定发送邮件的主机smtp.qq.com(QQ)|smtp.163.com(网易) 192.168.80.11

        Properties properties = System.getProperties();// 获取系统属性

        try {
            //QQ邮箱需要下面这段代码，163邮箱不需要
            properties.setProperty("mail.smtp.host", host); // 设置发送邮件的邮件服务器的属性（这里使用网易的smtp服务器）
            properties.put("mail.smtp.host", host); // 需要经过授权，也就是有户名和密码的校验，这样才能通过验证（一定要有这一条）
            properties.put("mail.smtp.auth", "true"); // 用刚刚设置好的props对象构建一个session
            properties.put("mail.mime.decodetext.stric", false);
            properties.put("mail.imap.auth.plain.disable", "true");
            properties.put("mail.imap.connectionpooltimeout", "6000");
            properties.put("mail.imap.connectionpoolsize", 4);


            // 1.获取默认session对象
            Session session = Session.getDefaultInstance(properties, new Authenticator() {
                public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("359642959@qq.com", "zacmsfziegiebjei"); // 发件人邮箱账号、授权码
                }
            });
            // 2.创建邮件对象
            Message message = new MimeMessage(session);
            // 2.1设置发件人
            message.setFrom(new InternetAddress(from));
            // 2.2设置接收人
            message.addRecipients(Message.RecipientType.TO, address);
            if(addressbbc.length > 0) {
                message.addRecipients(Message.RecipientType.BCC, addressbbc);
            }
            // 2.3设置邮件主题
            message.setSubject(subject);
            message.setContent(content, "text/html;charset=UTF-8");
            // 3.发送邮件
            Transport.send(message);

            System.out.println("邮件成功发送!!");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }




}
